package com.rla.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.rla.common.core.domain.R;
import com.rla.common.core.page.TableDataInfo;
import com.rla.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.rla.common.utils.StringUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.rla.domain.bo.BasePicUrlBo;
import com.rla.domain.vo.BasePicUrlVo;
import com.rla.domain.BasePicUrl;
import com.rla.mapper.BasePicUrlMapper;
import com.rla.service.IBasePicUrlService;

import java.util.*;

/**
 * 图片及跳转描述Service业务层处理
 *
 * @author rla
 * @date 2025-07-29
 */
@RequiredArgsConstructor
@Service
public class BasePicUrlServiceImpl implements IBasePicUrlService {

    private final BasePicUrlMapper baseMapper;

    /**
     * 查询图片及跳转描述
     */
    @Override
    public BasePicUrlVo queryById(String id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询图片及跳转描述列表
     */
    @Override
    public TableDataInfo<BasePicUrlVo> queryPageList(BasePicUrlBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<BasePicUrl> lqw = buildQueryWrapper(bo);
        Page<BasePicUrlVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询图片及跳转描述列表
     */
    @Override
    public List<BasePicUrlVo> queryList(BasePicUrlBo bo) {
        LambdaQueryWrapper<BasePicUrl> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<BasePicUrl> buildQueryWrapper(BasePicUrlBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<BasePicUrl> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getPicUrl()), BasePicUrl::getPicUrl, bo.getPicUrl());
        lqw.eq(StringUtils.isNotBlank(bo.getVideoUrl()), BasePicUrl::getVideoUrl, bo.getVideoUrl());
        lqw.eq(StringUtils.isNotBlank(bo.getJumpSet()), BasePicUrl::getJumpSet, bo.getJumpSet());
        lqw.eq(StringUtils.isNotBlank(bo.getJumpUrl()), BasePicUrl::getJumpUrl, bo.getJumpUrl());
        lqw.eq(StringUtils.isNotBlank(bo.getIsStopUse()), BasePicUrl::getIsStopUse, bo.getIsStopUse());
        lqw.eq(bo.getSort() != null, BasePicUrl::getSort, bo.getSort());
        lqw.eq(StringUtils.isNotBlank(bo.getType()), BasePicUrl::getType, bo.getType());
        lqw.orderBy(true,true,BasePicUrl::getSort);
        return lqw;
    }

    /**
     * 新增图片及跳转描述
     */
    @Override
    public Boolean insertByBo(BasePicUrlBo bo) {
        BasePicUrl add = BeanUtil.toBean(bo, BasePicUrl.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改图片及跳转描述
     */
    @Override
    public Boolean updateByBo(BasePicUrlBo bo) {
        BasePicUrl update = BeanUtil.toBean(bo, BasePicUrl.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(BasePicUrl entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除图片及跳转描述
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<String> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    public Map<String, Object> getAppIndexList(BasePicUrlBo bo) {
        Map<String, Object> result = new HashMap<>();

        bo.setType("1");
        bo.setIsStopUse("1");
        LambdaQueryWrapper<BasePicUrl> lqw = buildQueryWrapper(bo);
        List<BasePicUrlVo> bannerList = baseMapper.selectVoList(lqw);
        result.put("bannerList", bannerList);

        bo.setType("2");
        LambdaQueryWrapper<BasePicUrl> lqw1 = buildQueryWrapper(bo);
        List<BasePicUrlVo> jinGangList = baseMapper.selectVoList(lqw1);
        result.put("jinGangList", jinGangList);

        return result;
    }

    @Override
    public Map<String, Object> getAppExpertScienceList(BasePicUrlBo bo) {

        Map<String, Object> result = new HashMap<>();
        bo.setDelFlag("0");
        bo.setType("3");
        LambdaQueryWrapper<BasePicUrl> lqw = buildQueryWrapper(bo);
        List<BasePicUrlVo> teamList  = baseMapper.selectVoList(lqw);
        result.put("teamList", teamList);

        bo.setType("4");
        LambdaQueryWrapper<BasePicUrl> lqw1 = buildQueryWrapper(bo);
        List<BasePicUrlVo> synopsisList = baseMapper.selectVoList(lqw1);
        result.put("synopsisList", synopsisList);

        return result;
    }
}
